﻿@model ASC.Models.ChannelModel
@using ASC.Resources;
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<script>
    function channelTypeChanged(el) {
    
        var bt = $(el).find("option:selected").attr('bt');
        if(bt == 20)
        {
            $('#high').text('@Channel.OR');
            $('#low').text('@Channel.AND');
            
            $('#btnAddToMinus').removeAttr('disabled');
        }
        else if(bt == 3)
        {
            $('#high').text('@Channel.Mean');
            $('#low').text('@Channel.Sum');

            if($('input[name="plusMinus"]:checked').val() == 'high')
                $('#btnAddToMinus').attr('disabled', true);
            else
                $('#btnAddToMinus').removeAttr('disabled');
        }
        else
        {
            $('#btnAddToMinus').removeAttr('disabled');
        }
        //        $('input[type="textbox"][class^="BT"]:not(.BT' + bt + ')').val('');
        //        $('select[class^="BT"]:not(.BT' + bt + ')').val(-1);
        //        $('input[type="checkbox"][class^="BT"]:not(.BT' + bt + ')').attr('checked', false);
        $('[class^="BT"]:not(.BT' + bt + ')').attr('disabled', true);
        $('.BT' + bt).attr('disabled', false);

        transTypeChanged(document.getElementById('transtype'));

        $.ajax({
                  type: 'POST',
                  url: "@Url.Action("GetBaseChannels", "Channel")",
                  data: { baseTypeId: bt},
                  success: function(data){ 
                        $('#AvailableChannels option').remove();
                        if(data != null)
                        {
                            $.each(data.AvaribleChannels, function(index, val) {
                                $('#AvailableChannels').append(
                                    $('<option></option>').val(val.ID).html(val.Name)
                                );
                            });
                        }
                  },
                  dataType: 'json'
                 });

    }

    function transTypeChanged(el) {

        if ($(el).val() == 1 && !$(el).attr('disabled'))
            $('#Channel_Offset').removeAttr('disabled');
        else
            $('#Channel_Offset').attr('disabled', true);
    }

    $(function () {

        $(function () {
            $("#frmAddChannel").submit(function (e) {
                //$("#AvailableChannels option").attr("selected", "selected");
                $("#PlusChannels option").attr("selected", "selected");
                $("#MinusChannels option").attr("selected", "selected");

            });
        });

        $('#btnAddToPlus').click(function (e) {
            $('#AvailableChannels option:selected').appendTo('#PlusChannels');
            e.preventDefault();
        });

        $('#btnAddToMinus').click(function (e) {
            $('#AvailableChannels option:selected').appendTo('#MinusChannels');
            e.preventDefault();
        });

        $('#btnAddAllToPlus').click(function (e) {
            $('#AvailableChannels option').appendTo('#PlusChannels');
            e.preventDefault();
        });

        $('#btnAddToAvailable').click(function (e) {
            $('#MinusChannels option:selected').appendTo('#AvailableChannels');
            $('#PlusChannels option:selected').appendTo('#AvailableChannels');
            e.preventDefault();
        });
    });
    
    function ckeckPlusMinus(el)
    {
        var bt = $('#channelTypes').find("option:selected").attr('bt');
        
        if(bt == 3)
        {
            if($(el).filter(':checked').val() == 'high')
               $('#btnAddToMinus').attr('disabled', true);
            else
               $('#btnAddToMinus').removeAttr('disabled');
        }
        else
            $('#btnAddToMinus').removeAttr('disabled');
    }
   
</script>

@using (Html.BeginForm("Add", "Channel", FormMethod.Post, new { id = "frmAddChannel" }))
{
    <table style="width: 570px">
        <tr>
            <td class="centerAlign">
                <table class="maxWidth">
                    <tr>
                        <td class="centerAlign">
                            <h2>@Global.FrmChannelAdd</h2>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td>
                <table>
                    <tr>
                        <td class="rightAlign" style="width: 100px">
                            @Channel.ChannelName:
                        </td>
                        <td class="leftAlign">
                            @Html.TextBoxFor(x => x.Channel.Name)
                            @Html.ValidationMessageFor(x => x.Channel.Name)
                        </td>
                        <td class="rightAlign">
                            @Channel.No:
                        </td>
                        <td class="leftAlign">
                            <input type="text" style="width: 38px;" disabled="disabled" value="-1" />
                        </td>
                        <td class="rightAlign">
                            @Channel.Used:
                        </td>
                        <td>
                            @Html.CheckBoxFor(m => m.Channel.Used)
                        </td>
                        <td class="rightAlign" style="width: 100px">
                            @Channel.Type:
                        </td>
                        <td>
                            <select id="channelTypes" name="Channel.ChannelType_Id" style="width: 140px" onchange="channelTypeChanged(this)">
                                @foreach (var item in Model.ChannelTypes)
                                {
                                    <option value="@item.ChannelType_Id" bt="@item.BaseType">@item.Description</option>
                                }
                            </select>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td>
                <table>
                    <tr>
                        <td class="rightAlign" style="width: 100px">
                            @Channel.Description:
                        </td>
                        <td class="leftAlign">
                            @Html.TextBoxFor(x => x.Channel.Description, new { style = "width:190px;" })
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td>
                <table>
                    <tr>
                        <td>@Html.CheckBoxFor(m => m.Channel.SaveStat, new { @class = "BT1 BT2 BT4 BT3 BT20 BT5" })
                        </td>
                        <td style="width: 130px">@Channel.StoringStatistic
                        </td>
                        <td>@Html.CheckBoxFor(m => m.Channel.Sign, new { disabled = "disabled", @class = "BT2" })
                        </td>
                        <td style="width: 80px">@Channel.Remark
                        </td>
                        <td class="rightAlign" style="width: 100px">@Channel.Transformation:
                        </td>
                        <td>
                            <select id="transtype" name="Channel.Transtype" style="width: 140px" onchange="transTypeChanged(this)"
                                class="BT1">
                                <option value="-1"></option>
                                <option value="1">A/D conversion</option>
                                <option value="2">Peeling</option>
                                <option value="3">Pulse/value-conversion</option>
                            </select>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td>
                <table>
                    <tr>
                        <td>@Channel.ConnectTo:
                        </td>
                        <td>
                            <select name="Transtype" style="width: 140px;" disabled="disabled">
                                <option value="0"></option>
                            </select>
                        </td>
                        <td class="rightAlign" style="width: 80px">@Channel.SamplingInterval
                        </td>
                        <td>
                            @Html.TextBoxFor(x => x.Channel.Interval, new { style = "width:30px;", disabled = "disabled" })
                        </td>
                        <td style="width: 25px">@Channel.Sec
                        </td>
                        <td>
                            <table cellspacing="0" cellpadding="0">
                                <tr>
                                    <td>@Channel.Factor:
                                    </td>
                                    <td>@Html.TextBoxFor(x => x.Channel.Factor, new { style = "width:50px;" })
                                    </td>
                                </tr>
                                <tr>
                                    <td>@Channel.Offset:
                                    </td>
                                    <td>@Html.TextBoxFor(x => x.Channel.Offset, new { style = "width:50px;", disabled = "disabled", @class = "BT1" })
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td>
                <fieldset id="address" style="width: 560px">
                    <legend>@Channel.IO_Adress</legend>
                    <table>
                        <tr>
                            <td style="width: 80px">@Channel.WordLength
                            </td>
                            <td style="width: 80px">@Channel.Driver
                            </td>
                            <td style="width: 80px">@Channel.Loop
                            </td>
                            <td style="width: 80px">@Channel.Unit
                            </td>
                            <td style="width: 80px">@Channel.SubUnit
                            </td>
                            <td style="width: 80px">@Channel.Word
                            </td>
                            <td style="width: 80px">@Channel.Bit
                            </td>
                            <td style="width: 80px">@Channel.Inverted
                            </td>
                            <td style="width: 80px">@Channel.Async
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <select name="ChannelAddress.WordLength" style="width: 40px" class="BT1">
                                    <option value="-1"></option>
                                    <option value="0">4</option>
                                    <option value="1">8</option>
                                    <option value="2">16</option>
                                    <option value="3">32</option>
                                </select>
                            </td>
                            <td>
                                <select name="ChannelAddress.Drive" style="width: 60px" class="BT1 BT2 BT5">
                                    <option value="-1"></option>
                                    <option value="0">Test</option>
                                    <option value="1">Siox</option>
                                    <option value="2">PLCinPC</option>
                                    <option value="3">Profibus</option>
                                    <option value="4">DDE</option>
                                    <option value="5">Backhoff</option>
                                    <option value="6">ADSBackhoff</option>
                                    <option value="7">ADSBeSR</option>
                                    <option value="8">OPC_Beijer</option>
                                    <option value="9">SQLDriver</option>
                                    <option value="10">FileDriver</option>
                                    <option value="11">FileDriverB1</option>
                                </select>
                            </td>
                            <td>
                                @Html.TextBoxFor(m => m.ChannelAddress.Port, new { style = "width:50px;", @class = "BT1 BT2 BT5" })
                            </td>
                            <td>
                                @Html.TextBoxFor(m => m.ChannelAddress.Unit, new { style = "width:50px;", @class = "BT1 BT2 BT5" })
                            </td>
                            <td>
                                @Html.TextBoxFor(m => m.ChannelAddress.Subunit, new { style = "width:50px;", @class = "BT1 BT2 BT5" })
                            </td>
                            <td>
                                @Html.TextBoxFor(m => m.ChannelAddress.AddressByte, new { style = "width:50px;", @class = "BT1 BT2 BT5" })
                            </td>
                            <td>
                                @Html.TextBoxFor(m => m.ChannelAddress.AddressBit, new { style = "width:50px", disabled = "disabled", @class = "BT2 BT5" })
                            </td>
                            <td>@Html.CheckBoxFor(m => m.ChannelAddress.Inverted, new { disabled = "disabled", @class = "BT2 BT20" })
                            </td>
                            <td>@Html.CheckBoxFor(m => m.ChannelAddress.AsyncType, new { @class = "BT1 BT4 BT3 BT2 BT20" })
                            </td>
                        </tr>
                    </table>
                </fieldset>
            </td>
        </tr>
        <tr>
            <td>
                <fieldset style="width: 560px">
                    <legend>Channels</legend>
                    <table>
                        <tr>
                            <td style="width: 140px" class="centerAlign">
                                @Channel.AvailableChannels
                            </td>
                            <td style="width: 140px">
                                <input type="radio" name="plusMinus" disabled="disabled" checked="checked"
                                    class="BT3 BT20" value="high" onchange="ckeckPlusMinus(this)" />&nbsp;<span id="high">@Channel.Mean</span>
                            </td>
                            <td colspan="2" class="centerAlign">
                                @Channel.SelectedChannels
                            </td>
                        </tr>
                        <tr>
                            <td>
                            </td>
                            <td>
                                <input type="radio" name="plusMinus" disabled="disabled" class="BT3 BT20" value="low"
                                    onchange="ckeckPlusMinus(this)" />&nbsp;<span id="low">@Channel.Sum</span>
                            </td>
                            <td style="width: 140px" class="centerAlign">
                                @Channel.Plus
                            </td>
                            <td style="width: 140px" class="centerAlign">
                                @Channel.Minus
                            </td>
                        </tr>
                        <tr>
                            <td>
                                @Html.ListBox("AvailableChannels", new SelectList(Model.AvailableChannels, "Channel_Id", "Name"), new { id = "AvailableChannels", style = "height:150px; width:130px;overflow:auto" })
                            </td>
                            <td>
                                <table>
                                    <tr>
                                        <td>
                                            <input type="button" id="btnAddToPlus" value="=> +" class="StandartButton" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <input type="button" id="btnAddToMinus" value="=> -" class="StandartButton" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <input type="button" id="btnAddAllToPlus" value="=>> +" class="StandartButton" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <input type="button" id="btnAddToAvailable" value='&lt;=' class="StandartButton" />
                                        </td>
                                    </tr>
                                </table>
                            </td>
                            <td>
                                @Html.ListBox("PlusChannels", new SelectList(Model.PlusChannels, "Channel_Id", "Name"), new { id = "PlusChannels", style = "height:150px; width:130px;overflow:auto" })
                            </td>
                            <td>
                                @Html.ListBox("MinusChannels", new SelectList(Model.MinusChannels, "Channel_Id", "Name"), new { id = "MinusChannels", style = "height:150px; width:130px;overflow:auto" })
                            </td>
                        </tr>
                    </table>
                </fieldset>
            </td>
        </tr>
        <tr>
            <td class="rightAlign">
                <table class="maxWidth">
                    <tr>
                        <td class="rightAlign">
                            <input type="submit" name="Save" value="@Global.Save" class="StandartButton" />
                        </td>
                        <td class="leftAlign">
                            <input type="button" name="Cancel" value="@Global.Cancel" onclick="window.location.href='/'" class="StandartButton" />
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table> 
}
